package com.sdhs.util;

import com.aliyuncs.utils.StringUtils;
import com.sdhs.common.core.domain.entity.SysUser;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.common.utils.spring.SpringUtils;
import com.sdhs.myproject.domain.GcMyprojectContractInformation;
import com.sdhs.myproject.domain.GcMyprojectInformation;
import com.sdhs.myproject.service.IGcMyprojectInformationService;
import com.sdhs.safetymanage.domain.SafetyProject;
import com.sdhs.safetymanage.service.ISafetyProjectService;
import com.sdhs.system.mapper.SysUserMapper;
import com.sdhs.system.service.ISysDeptService;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


public class SafetyProjectUnit {

    private static final ISafetyProjectService safetyProjectService = SpringUtils.getBean(ISafetyProjectService.class);
    private static final IGcMyprojectInformationService myProjectInformationService = SpringUtils.getBean(IGcMyprojectInformationService.class);
    private static final SysUserMapper sysUserMapper = SpringUtils.getBean(SysUserMapper.class);
    private static final ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
    public static Long addSafetyProject(GcMyprojectInformation gcMyprojectInformation) {

         SafetyProject safetyProject = new SafetyProject();
         String creater = null;
         if(!StringUtils.isEmpty(gcMyprojectInformation.getCreater())){
             creater = gcMyprojectInformation.getCreater();
         }else {
             creater = SecurityUtils.getUsername();
         }
        SysUser sysUser = sysUserMapper.selectUserByUserName(creater);
         if(!StringUtils.isEmpty(sysUser.getDeptId())){
              String s = sysDeptService.selectCompanyIdtByUserId(sysUser.getDeptId());
             safetyProject.setCompanyId(s);
         }
        SafetyProject safetyProject1 = safetyProjectService.selectSafetyProjectByCreate(creater);
        safetyProject.setPm(safetyProject1.getId());//项目经理人员id（sf_person表的id）
        safetyProject.setPmLink(safetyProject1.getPhonenumber());//项目经理联系方式
        safetyProject.setNo(gcMyprojectInformation.getProjectNumber()); //项目编号
        safetyProject.setName(gcMyprojectInformation.getProjectName());//项目名称
        safetyProject.setShortName(gcMyprojectInformation.getProjectName());//项目简称---和项目名称是一样的
        safetyProject.setStatus("1");//项目状态 -施工
        safetyProject.setStartDate(gcMyprojectInformation.getPlanStartTime());//项目开始时间
        safetyProject.setEndDate(gcMyprojectInformation.getPlanEndTime()); //项目截止时间
        safetyProject.setCreateTime(new Date());
        safetyProject.setCreateBy(creater);

        List<GcMyprojectContractInformation> gcMyprojectContractInformationList = gcMyprojectInformation.getGcMyprojectContractInformationList();
        if (gcMyprojectContractInformationList != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 调整格式
            for (GcMyprojectContractInformation contract : gcMyprojectContractInformationList) {
                if (contract != null && "收入类".equals(contract.getIncomeExpenseType())) {
                    try {
                        LocalDate localDate = LocalDate.parse(contract.getSignDate(), formatter);
                        Date signDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
                        safetyProject.setSignDate(signDate);
                    } catch (Exception e) {
                        e.printStackTrace(); // 处理日期解析错误
                    }
                }
            }
        }


         safetyProjectService.insertSafetyProject(safetyProject);//插入项目
        return safetyProject.getId();
/*         Long id = safetyProject.getId();
        myProjectInformationService.updateGcMyProject(gcMyprojectInformation); //更新项目*/
    }
}
